import { Router } from 'express';
import * as fs from 'fs';
import * as formidable from 'formidable';
const router = Router();
import connect from './atom/connect';
import cross from './atom/cross-domain';

(async () => {
	cross(router);

	const mysql = await connect('localhost', 'root', 'msz', 'test');
	// const mysql = await connect('140.143.122.74', 'root', 'msz', 'test');


	router.get('/add', function (req, res) {
		res.header('Access-Control-Allow-Origin', '*');
		const data = req.query;

		const addSql = "INSERT INTO runoob_tbl(runoob_id,runoob_author,runoob_title,submission_date) VALUES('" + data.runoob_id + "','" + data.runoob_author + "','" + data.runoob_title + "',now())";
		// const data = ['6', '马士振', 'title', new Date()];
		mysql.query(addSql, data, function (err: any, result: any[]) {
			if (err) {
				console.log('[INSERT ERROR] - ', err.message);
				res.send(err.message);
				return;
			}
			res.send(result);
		});
	});
	router.get('/find', function (req, res) {
		res.header('Access-Control-Allow-Origin', '*');

		mysql.query('select * from runoob_tbl;', function (err: string, result: any[]) {
			res.send(result);
		});
	});
	router.post('/find-all', function (req, res) {
		res.header('Access-Control-Allow-Origin', '*');

		mysql.query('select * from runoob_tbl;', function (err: string, result: any[]) {
			res.send({ data: result });
		});
	});

	router.post('/upload', function (req, res) {
		const form = new formidable.IncomingForm();
		form.uploadDir = __dirname + '/../public/image';
		form.keepExtensions = true;		// 保留后缀

		form.parse(req, function (err, fields, files) {
			if (err) { throw err }
			const new_name = new Date().getTime() + '.' + files.file.name;
			res.send("./image/" + new_name);
			console.log(form.uploadDir, files.file.path);
			const new_path = form.uploadDir + '/' + new_name;
			fs.renameSync(files.file.path, new_path); // 重命名
		});
	});

	router.post('/form', function (req, res) {
		res.header('Access-Control-Allow-Origin', '*');
		const form = new formidable.IncomingForm();
		form.keepExtensions = true;		// 保留后缀
		const post = {} as { [field: string]: string | number | boolean };
		form.uploadDir = __dirname + '/../../public/image';
		form
			.on('error', function (err) {
				console.log(err); //各种错误
			})
			// POST 普通数据 不包含文件 field 表单name value 表单value
			.on('field', function (field, value) {
				console.log('======>>>>>>> field', field);
				console.log('======>>>>>>> value', value);
				if (form.type == 'multipart') {  // 有文件上传时 enctype="multipart/form-data"
					post[field] = value;
				}
				post[field] = value;
			})
			.on('file', function (field, file) { // 上传文件
				console.log(form.uploadDir, file.path)
				const new_name = new Date().getTime() + '.' + file.name;
				const new_path = form.uploadDir + '/' + new_name;
				fs.renameSync(file.path, new_path); // 重命名
				post[field] = '/image/' + new_name;
			})
			.on('end', function () {
				console.log('解析post数据完毕');
				res.send(post);
			});
		form.parse(req); // 解析request对象
	});

	console.log('1111111111111111');
	router.post('/post-test', function (req, res) {
		res.header('Access-Control-Allow-Origin', '*');
		const query = req.body;
		res.send(query);
	});

	router.get('/get-test', function (req, res) {
		res.header('Access-Control-Allow-Origin', '*');
		const query = req.query;
		res.send(query);
	});

	router.post('/post-demo', (req, res) => {
		res.header('Access-Control-Allow-Origin', '*');
		console.log(req.body);
		res.send(req.body);
	});

	// router.get('/test-get', test_get(req, res));
})();
export default router;
